/**
* Copyright (c) 2016-2100 yunclouds
*/
package com.yunqian.web;

import com.yunqian.common.CurrentUser;
import com.yunqian.jwt.JwtUser;
import com.yunqian.redot.constant.Operation;
import com.yunqian.redot.constant.OperationMenus;
import com.yunqian.redot.domain.InformationType;
import com.yunqian.redot.service.InformationTypeService;
import com.yunqian.redot.service.OperationLogService;
import com.yunqian.util.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Describe: 咨询分类管理控制器
 *
 * @author zsy
 * 2018-09-29 15:54:40
 */
 @RestController
 @RequestMapping("/api/informationType")
public class InformationTypeController {

     @Autowired
     private InformationTypeService informationTypeService;
     @Autowired
     private OperationLogService operationLogService;

    /**
     * 获取分页
     *
     * @param params
     * @return
     */
    @PostMapping("/pagination")
    public Page<InformationType> pagination(@RequestBody Map<String, Object> params) {
        return informationTypeService.pagination(params);
    }

     /**
     * 获取所有列表
     *
     * @return
     */
    @GetMapping("/all")
    public Map<String, Object> list() {
        List<InformationType> informationTypeList = informationTypeService.findAllByOrderBySortNum();
        Map<String, Object> map = new HashMap<>(16);
        map.put("informationTypeList", informationTypeList);
        return map;
    }

    /**
     * 添加分类
     *
     * @param informationType 对象信息
     */
    @PostMapping
    public InformationType save(@RequestBody @Valid InformationType informationType, @CurrentUser JwtUser user) {
        informationTypeService.save(informationType, user.getId());
        return informationType;
    }

    /**
     * 隐藏/显示
     *
     * @param id         待更新id
     * @param modifyData 待更新的数据
     * @return
     */
    @PutMapping("/{id}")
    public InformationType update(@PathVariable("id") Long id, @RequestBody Map<String, Object> modifyData, @CurrentUser JwtUser user) {
        InformationType type = informationTypeService.update(id, modifyData);
        if(type.getStatus() == 2){
            operationLogService.save(OperationMenus.NEWS_TYPE, Operation.SHOW, user.getId());
        }else {
            operationLogService.save(OperationMenus.NEWS_TYPE, Operation.HIDE, user.getId());
        }
        return type;
    }

    /**
     * 编辑分类
     * @return
     */
    @PutMapping
    public InformationType update(@RequestBody @Valid InformationType informationType, @CurrentUser JwtUser user) {
        InformationType origType = informationTypeService.getById(informationType.getId());
        BeanUtil.copyNotNullProperties(origType, informationType);
        return informationTypeService.update(origType, user.getId());
    }

    /**
     * 根据id获取详情
     *
     * @param id 要查询的id
     * @return
     */
    @GetMapping("/{id}")
    public InformationType detail(@PathVariable("id") Long id) {
        return informationTypeService.findStrictOne(id);
    }


}
